<?php
declare (strict_types=1);

namespace app\common\dao\activity\lottery;

use app\common\dao\BaseDao;
use app\common\model\activity\lottery\LuckLottery;


/**
 *
 * Class LuckLotteryDao
 * @package app\common\dao\activity\lottery
 */
class LuckLotteryDao extends BaseDao
{


    /**
     * @return string
     * @author: fupeng
     */
    protected function getModel(): string
    {
        return LuckLottery::class;
    }

    /**
     * @param array $where
     * @return \think\db\BaseQuery
     * @author: fupeng
     * 抽奖搜索
     */
    public function search(array $where)
    {
        $query = LuckLottery::getDB();
        $query->when(isset($where['id']) && $where['id'], function ($query) use ($where) {
                $query->where('id', $where['id']);
            })->when(isset($where['start']) && $where['start'] !== '', function ($query) use ($where) {
                $time = time();
                switch ($where['start']) {
                    case 0:
                        $query->where('start_time', '>', $time)->where('status', 1);
                        break;
                    case -1:
                        $query->where(function ($query1) use ($time) {
                            $query1->where('end_time', '<', $time)->whereOr('status', 0);
                        });
                        break;
                    case 1:
                        $query->where('status', 1)->where(function ($query1) use ($time) {
                            $query1->where(function ($query2) use ($time) {
                                $query2->where('start_time', '<=', $time)->where('end_time', '>=', $time);
                            })->whereOr(function ($query3) {
                                $query3->where('start_time', 0)->where('end_time', 0);
                            });
                        });
                        break;
                }
            });
        $query->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
            getModelTime($query, $where['date']);
        });
        return $query->where('is_del',0);






    }

    /**
     * @param array $where
     * @param string $field
     * @param array $with
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @day: 2024/3/28
     * 抽奖活动列表
     */
    public function getList(array $where, string $field = '*', array $with = [], int $page = 0, int $limit = 0)
    {
        return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->when($page && $limit, function ($query) use ($page, $limit) {
            $query->page($page, $limit);
        })->order('add_time desc')->select()->toArray();
    }

    /**
     * @param int $id
     * @param string $field
     * @param array $with
     * @param bool $is_doing
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @day: 2024/3/28
     * 获取单个活动
     */
    public function getLottery(int $id, string $field = '*', array $with = ['prize'], bool $is_doing = false)
    {
        $where = ['id' => $id];
        $where['is_del'] = 0;
        if ($is_doing) $where['start'] = 1;
        return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->find();
    }

    /**
     * @param int $factor
     * @param string $field
     * @param array $with
     * @param bool $is_doing
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @day: 2024/3/28
     * 获取某个抽奖类型的一条抽奖数据
     */
    public function getFactorLottery(int $factor = 1, string $field = '*', array $with = ['prize'], bool $is_doing = false)
    {
        $where = ['factor' => $factor, 'is_del' => 0];
        if ($is_doing) $where['start'] = 1;
        return $this->search($where)->field($field)->when($with, function ($query) use ($with) {
            $query->with($with);
        })->find();
    }
}
